<!-- eslint-disable vue/multi-word-component-names -->
<template>
    <div class="box">

        <van-form v-model="fromData">
            <van-cell-group inset>
                <van-field v-model="fromData.username" name="用户名" label="用户名" placeholder="用户名"
                    :rules="[{ required: true, message: '请填写用户名' }]" />
                <van-field v-model="fromData.password" type="password" name="密码" label="密码" placeholder="密码"
                    :rules="[{ required: true, message: '请填写密码' }]" />
            </van-cell-group>
            <div style="margin: 1rem;">
                <van-button round block type="primary" native-type="submit" @click="login">
                    登录
                </van-button>
            </div>
        </van-form>
    </div>

</template>
<script setup lang="ts">

import axios from "axios"

import { ref } from 'vue'
import { showToast } from 'vant';
import {useRouter} from 'vue-router'
const router=useRouter()
const fromData=ref({
    username:'',
    password:''
})
const login=async()=>{
    let res=await axios.post('/v1/login',fromData.value)
    const {data,message,code}=res.data
    console.log(res);
    
    if(code===10000){
        localStorage.setItem('userInfo',JSON.stringify(data.userInfo))
        localStorage.setItem('accessToken', data.accessToken)
        localStorage.setItem('refreshToken', data.refreshToken)
        router.push('/order')
        showToast({
            message:'登录成功',
            type:'success'
        })
    }
    else{
        showToast({
            message:message,
            type:'fail'
        })
    }

    console.log(res)
    
}

</script>
<style lang="scss" scoped>
 .box{
    width: 350px;
    background-color: #fff;
    border-radius: 5px;
    box-shadow: 0 2px 5px rgba(0, 0, 0, 0.2);
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);

 
 }

</style>